package com.rpay.service.base.impl;

import com.github.pagehelper.PageHelper;
import com.rpay.service.base.IBaseService;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

import java.util.List;


public abstract class BaseServiceImpl<T> implements IBaseService {
    @Autowired
    protected Mapper<T> mapper;
    @Autowired
    protected MySqlMapper<T> mySqlMapper;

    @Autowired
    @Qualifier("readSqlSession")
    private SqlSessionTemplate readSqlSession;

    public SqlSessionTemplate getSqlSessionTemplate() {
        return readSqlSession;
    }

    @Override
    public long saveModel(Object model) {
        return mapper.insertSelective((T) model);
    }

    @Override
    public void modModelByPrimaryKey(Object model) {
        mapper.updateByPrimaryKeySelective((T) model);
    }

    /**
     * 单表分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public List<T> selectPage(T record, int pageNum, int pageSize) {
        //pagehelper 不生效
//        PageHelper.startPage(pageNum, pageSize);
        //Spring4支持泛型注入
        return mapper.select(record);
    }

    @Override
    public int deleteBySql(String statement, Object o) {
       return readSqlSession.delete(statement, o);
    }

    @Override
    public void saveModelList(List model) {
        mySqlMapper.insertList((List<T>) model);
    }

    @Override
    public Object getModelById(Object Id) {
        return mapper.selectByPrimaryKey(Id);
    }

}
